<?php

FILESAVER;

/**
 * Description of config
 *
 * @author adrian
 */
class Config {

    /**
     *
     * @var Config 
     */
    private static $instance = null;
    /**
     *
     * @var array
     */
    private $config = array();

    /**
     *
     * @return Config
     */
    public static function instance() {
        if (self::$instance == null) {
            self::$instance = new Config();
        }
        return self::$instance;
    }

    /**
     *  :)
     */
    private function __construct() {
        ;
    }

    /**
     * Ładuje configi modułów i komponentów
     */
    public function load_configs() {
        require System::search('system', 'config');
        $this->config = array_merge($this->config, $config);
        $t = array(MOD_PATH, COM_PATH);
        foreach ($t as $directory) {
            $this->file_check($directory);
        }
    }

    /**
     * 
     * @param string $name
     * @param string $element
     * @return mixed
     */
    public static function get_value($name, $element = null) {
        if ($element == null)
            return $this->config[$name];
        return $this->config[$name][$element];
    }

    /**
     *
     * @param string $path
     */
    private function file_check($path) {
        $dir = opendir($path);
        while ($file = readdir($dir)) {
            if ($file == '.' || $file == '..')
                continue;
            elseif ($file == 'config') {
                $configdir_handle = opendir($path . '/' . $file . '/');
                while ($configdir_file = readdir($configdir_handle)) {
                    if ($configdir_file == '.' || $configdir_file == '..')
                        continue;
                    if (is_file($path . '/' . $file . '/' . $configdir_file)) {
                        require $path . '/' . $file . '/' . $configdir_file;
                        $this->config = array_merge($this->config, $config);
                    }
                }
            } elseif (is_dir($path . $file)) {
                $this->file_check($path . $file);
            }
        }
    }

}
?>
